package com.archgeek.java.conc.synchro;

import org.checkerframework.checker.units.qual.A;
import org.junit.Test;

import java.io.IOException;
import java.util.concurrent.TimeUnit;

/**
 * @author pizhihui
 * @date 2024-03-20 11:16
 */
public class SynLock8Demo {

    /**
     * 线程   操作  资源类  8锁案例说明
     * 1    标准访问有ab两个线程，请问先打印邮件还是短信
     * 2    sendEmail方法暂停3秒钟，请问先打印邮件还是短信
     * 3    新增一个普通的hello方法，请问先打印邮件还是hello
     * 4    有两部手机，请问先打印邮件还是短信
     * 5    两个静态同步方法，同1部手机，请问先打印邮件还是短信
     * 6    两个静态同步方法， 2部手机，请问先打印邮件还是短信
     * 7    1个静态同步方法，1个普通同步方法,同1部手机，请问先打印邮件还是短信
     * 8    1个静态同步方法，1个普通同步方法,2部手机，请问先打印邮件还是短信
     */

    @Test
    public void test1() {

        class Phone {
            public synchronized void sendEmail() {
                System.out.println("----------sendEmail");
            }

            public synchronized void sendSms() {
                System.out.println("----------sendSms");
            }
        }

        Phone phone = new Phone();
        new Thread(phone::sendEmail, "a").start();
        new Thread(phone::sendSms, "b").start();


    }

    @Test
    public void test2() throws IOException {

        class Phone {
            public synchronized void sendEmail() {
                sleep(3000);
                System.out.println("----------sendEmail");
            }

            public synchronized void sendSms() {
                System.out.println("----------sendSms");
            }
        }

        Phone phone = new Phone();
        new Thread(phone::sendEmail, "a").start();
        new Thread(phone::sendSms, "b").start();

        System.in.read();

    }


    @Test
    public void test3() throws IOException {
        class Phone {
            public synchronized void sendEmail() {
                sleep(3000);
                System.out.println("----------sendEmail");
            }

            public synchronized void sendSms() {
                System.out.println("----------sendSms");
            }

            public void hello() {
                System.out.println("----------hello");
            }
        }

        Phone phone = new Phone();
        new Thread(phone::sendEmail, "a").start();
        new Thread(phone::hello, "b").start();

        System.in.read();

    }

    @Test
    public void test4() throws IOException {
        class Phone {
            public synchronized void sendEmail() {
                sleep(3000);
                System.out.println("----------sendEmail");
            }

            public synchronized void sendSms() {
                System.out.println("----------sendSms");
            }

            public void hello() {
                System.out.println("----------hello");
            }
        }

        Phone phone = new Phone();
        Phone phone2 = new Phone();
        new Thread(phone::sendEmail, "a").start();
        new Thread(phone2::sendSms, "b").start();

        System.in.read();

    }



    @Test
    public void test5() throws IOException {

//        Phone phone = new Phone();
//        new Thread(phone::sendEmail, "a").start();
//        new Thread(phone::sendSms, "b").start();

        System.in.read();

    }

    public static void main(String[] args) {
        Phone phone = new Phone();
        Phone phone2 = new Phone();
//        new Thread(phone.sendEmail(), "a").start();
//        new Thread(phone::sendSms(), "b").start();

        new Thread(() -> {
            phone.sendEmail();
        }, "a").start();

        new Thread(() -> {
            phone2.hello();
        }, "a").start();


    }




    private static void sleep(long milli) {
        try {
            TimeUnit.MILLISECONDS.sleep(milli);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}


class Phone {

    public static synchronized void sendEmail() {
        sleep(3000);
        System.out.println("----------sendEmail");
    }

    public static synchronized void sendSms() {
        System.out.println("----------sendSms");
    }

    public void hello() {
        System.out.println("----------hello");
    }
    private static void sleep(long milli) {
        try {
            TimeUnit.MILLISECONDS.sleep(milli);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
